import Vue from 'vue'
import dataUtils from '../utils/data.js'
//金额过滤
Vue.filter('money', function(val,unit='yuan') {
	//console.log(val);
	let w = unit=='fen'?1:100;
	if (val) {
		let value = Math.round(parseFloat(val) * w) / 100;
		let valMoney = value.toString().split(".");
		if (valMoney.length == 1) {
			value = value.toString() + ".00";
			return value;
		}
		if (valMoney.length > 1) {
			if (valMoney[1].length < 2) {
				value = value.toString() + "0";
			}
			return value;
		}
		return value;
	} else {
		return "0.00";
	}
});
Vue.filter('max99', function(val) {
	//console.log(val);
	if (val) {
		let value = parseInt(val);
		
		if (value > 99) {
			value = value.toString() + "+";
		}
		return value;
	} else {
		return "0";
	}
});
// 时间距离现在多少天前
Vue.filter('dateDiff', function(val) {
	if (val) {
		return dataUtils.clickDateDiff(val);
	} else {
		return "";
	}
});
const timeFormat =function(val, fmt = 'yyyy-MM-dd hh:mm:ss') {
	//console.log('timeFormat',typeof val);
	if (val) {
		if(typeof val == "number" || parseFloat(val).toString() !== "NaN"){
			if (String(val).length==10){
				val = val*1000;
			}
			return new Date(parseFloat(val)).format(fmt);
		}else if(typeof val == "string"){
			if (val=='now'){
				return new Date().format(fmt);				
			}else{
				let timeText = val.replace(/-/g, "/");
				return new Date(timeText).format(fmt);				
			}
		} 
	} else {
		return "";
	}
}
Vue.filter('dateFormat', function(val, fmt = 'yyyy-MM-dd') {
	return timeFormat(val,fmt);
});
//时间格式化
Vue.filter('timeFormat1', timeFormat);
//隐藏手机号
Vue.filter('hidePhone', function(val) {
	return val.substr(0,5) + '***' + val.substr(8)
});
